package com.xmtlxx.web.controller.utils;

import com.xmtlxx.common.tengbo.PushDatasUtil;
import com.xmtlxx.common.tengbo.vo.OrderModelsVo;
import com.xmtlxx.common.utils.JsonUtils;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.oms.domain.bo.OperatorLogBo;
import com.xmtlxx.oms.domain.vo.OperatorLogVo;
import com.xmtlxx.oms.domain.vo.StagingVo;
import com.xmtlxx.oms.mapper.OperatorLogMapper;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.mapper.SysOssMapper;
import com.xmtlxx.tms.domain.vo.OrderVo;
import com.xmtlxx.tms.domain.vo.WaybillDtlVo;
import com.xmtlxx.tms.domain.vo.WaybillLogVo;
import com.xmtlxx.tms.domain.vo.WaybillVo;
import com.xmtlxx.tms.mapper.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderUtils {

    private final OrderMapper baseMapper;

    private final OrderDtlMapper orderDtlMapper;

    private final OrderLogMapper orderLogMapper;

    private final WaybillMapper waybillMapper;

    private final WaybillDtlMapper waybillDtlMapper;

    private final WaybillLogMapper waybillLogMapper;

    private final SysOssMapper ossMapper;

    private final OperatorLogMapper operatorLogMapper;


    //订单/运单数据推送
    public  String pushOrderInfo(String orderId){
        try {
            OrderVo vo = baseMapper.selectOrderVoByID(orderId);
            //订单明细
            vo.setItems(orderDtlMapper.selectOrderDtls(orderId));

            //操作日志
            OperatorLogBo operatorLogBo = new OperatorLogBo();
            operatorLogBo.setObjid(vo.getTaskId());
            operatorLogBo.setLogType("tms");
            List<OperatorLogVo> operatorLogVos = operatorLogMapper.selectOperatorLogList(operatorLogBo);
            if(operatorLogVos != null && operatorLogVos.size() > 0){
                vo.setOperatorLogs(operatorLogVos);
            }

            //子订单(暂存单时)
            List<OrderVo> orderItems = baseMapper.selectOrderByFid(vo.getId());
            if(orderItems != null && orderItems.size() > 0){
                orderItems.forEach(item->{
                    item.setItems(orderDtlMapper.selectOrderDtls(item.getId()));
                    item.setOrderLogs(orderLogMapper.selectOrderLogs(item.getId()));

                    List<WaybillVo> waybillItems = waybillMapper.selectWaybillVoByOrderId(item.getId());
                    waybillItems.forEach(waybill->{
                        List<WaybillDtlVo> dtls = waybillDtlMapper.selectWaybillDtls(waybill.getId());
                        if(dtls != null && dtls.size() >0){
                            waybill.setItems(dtls);
                        }
                        List<WaybillLogVo> logItems = waybillLogMapper.selectWaybillLogs(item.getId());
                        if(logItems != null && logItems.size() > 0){
                            waybill.setLogItems(logItems);
                        }
                    });
                    item.setWayItems(waybillItems);

                    //车架号
                    SysOssBo ossBo = new SysOssBo();
                    ossBo.setObjid(orderId);
                    ossBo.setBuss("carNumber");
                    item.setCarOss(ossMapper.selectSysOssList(ossBo));

                    //交接现场
                    ossBo.setBuss("handover");
                    item.setHandoverOss(ossMapper.selectSysOssList(ossBo));

                    //装车
                    ossBo.setBuss("load");
                    item.setLoadOss(ossMapper.selectSysOssList(ossBo));

                    //目的地
                    ossBo.setBuss("take");
                    item.setTakeOss(ossMapper.selectSysOssList(ossBo));

                    //卸车
                    ossBo.setBuss("unload");
                    item.setUnloadOss(ossMapper.selectSysOssList(ossBo));

                    //其他
                    ossBo.setBuss("other");
                    item.setOtherOss(ossMapper.selectSysOssList(ossBo));
                });

                vo.setOrderItems(orderItems);
            }
            //订单日志
            vo.setOrderLogs(orderLogMapper.selectOrderLogs(orderId));
            //运单
            List<WaybillVo> waybillItems = waybillMapper.selectWaybillVoByOrderId(orderId);
            waybillItems.forEach(item->{
                List<WaybillDtlVo> items = waybillDtlMapper.selectWaybillDtls(item.getId());
                if(items != null && items.size() >0){
                    item.setItems(items);
                }
                List<WaybillLogVo> logItems = waybillLogMapper.selectWaybillLogs(item.getId());
                if(logItems != null && logItems.size() > 0){
                    item.setLogItems(logItems);
                }
            });
            vo.setWayItems(waybillItems);



            //车架号
            SysOssBo ossBo = new SysOssBo();
            ossBo.setObjid(orderId);
            ossBo.setBuss("carNumber");
            vo.setCarOss(ossMapper.selectSysOssList(ossBo));

            //交接现场
            ossBo.setBuss("handover");
            vo.setHandoverOss(ossMapper.selectSysOssList(ossBo));

            //装车
            ossBo.setBuss("load");
            vo.setLoadOss(ossMapper.selectSysOssList(ossBo));

            //目的地
            ossBo.setBuss("take");
            vo.setTakeOss(ossMapper.selectSysOssList(ossBo));

            //卸车
            ossBo.setBuss("unload");
            vo.setUnloadOss(ossMapper.selectSysOssList(ossBo));

            //其他
            ossBo.setBuss("other");
            vo.setOtherOss(ossMapper.selectSysOssList(ossBo));

            OrderModelsVo data = new OrderModelsVo();
            data.setApi("logistics");
            data.setData(vo);
            log.info("PushTMSOrderDatas Datas = >{}", JsonUtils.toJsonString(data));
            PushDatasUtil.addTask("logistics",data);
        }catch (Exception e){
            e.printStackTrace();
            log.error("PushTMSOrderDatas Error = >{}",e.getMessage());
        }
        return null;
    }

    public static void main(String[] args) {
        String filePath = "/Users/rony/prop.xlsx";
        try {
            InputStream in = new FileInputStream(filePath);
            List<StagingVo> items = ExcelUtil.importExcel(in,StagingVo.class);
            System.out.println(items.size());
            StringBuilder sb = new StringBuilder();
            for(StagingVo item : items){
                if(!StringUtils.isEmpty(item.getPno()) && !StringUtils.isEmpty(item.getTransferDeptName()) && !item.getTransferDeptName().equals("NULL")){
                    sb.append("UPDATE XMTLXX_DEV.T_PROPERTY SET BELONG_DEPT_NAME =(SELECT DEPT_NAME FROM  XMTLXX_DEV.SYS_DEPT WHERE DEPT_CODE = '"+item.getTransferDeptName()+"')  WHERE PNO ='"+item.getPno()+"' AND BELONG_DEPT_NAME IS  NULL;");
//                    sb.append("UPDATE XMTLXX_DEV.T_STAGING SET TRANSFER_DEPT_NAME =(SELECT DEPT_NAME FROM  XMTLXX_DEV.SYS_DEPT WHERE DEPT_CODE = '"+item.getTransferDeptName()+"') WHERE PNO ='"+item.getPno()+"' AND TRANSFER_DEPT_NAME IS  NULL;");
                    sb.append("\r\n");
                }
            }
            String outPath = "/Users/rony/prop.text";
            FileOutputStream fos = new FileOutputStream(outPath);

            // 将字符串转换为字节数组
            byte[] bytes = sb.toString().getBytes();

            // 将字节数组写入文件
            fos.write(bytes);

            // 关闭文件输出流
            fos.close();

        }catch (IOException e){
            e.printStackTrace();
        }

    }

}
